import Vue from 'vue'
import VueI18n from 'vue-i18n'
import axios from 'axios'
import VueAxios from 'vue-axios'
import VueAuth from '@websanova/vue-auth'
import NProgress from 'vue-nprogress'
import { sync } from 'vuex-router-sync'
import App from './App.vue'
import router from './router'
import store from './store'
import * as filters from './filters'
import { TOGGLE_SIDEBAR } from 'vuex-store/mutation-types'
import echarts from 'echarts'
import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'
import LangEn from '../static/lang/en'
import LangZhCHS from '../static/lang/zhCHS'
Vue.prototype.$echarts = echarts
Vue.prototype.STORE_PATH = '/home/kirohuji/桌面/数据分析系统/系统/www/store'
Vue.router = router
// Vue.use 注册全局组件
Vue.use(VueAxios, axios)
Vue.use(mavonEditor)
Vue.use(VueI18n)
// 跟数据相关
Vue.use(VueAuth, {
  auth: {
    request: function (req, token) {
      console.log('请求头的Authorization' + token)
      this.options.http._setHeaders.call(this, req, {Authorization: window.localStorage.getItem('auth-token')})
    },
    response: function (res) {
      console.log(res)
      // Get Token from response body
      return res.data
    }
  },
  http: require('@websanova/vue-auth/drivers/http/axios.1.x.js'),
  router: require('@websanova/vue-auth/drivers/router/vue-router.2.x.js'),
  loginData: { url: 'http://139.224.118.74:8080/query/sign/in', fetchUser: false },
  refreshData: { enabled: false }
})

Vue.use(NProgress)

// Enable devtools
// 开启调试
Vue.config.devtools = true
// Effortlessly keep vue-router and vuex store in sync.
// 毫不费力地保持vue-router 和 vuex的store的同步
sync(store, router)

const nprogress = new NProgress({ parent: '.nprogress-container' })
// 从vuex中获取状态树
const { state } = store
// 判断是否移动端或者是左侧栏是否显示
router.beforeEach((route, redirect, next) => {
  if (state.app.device.isMobile && state.app.sidebar.opened) {
    store.commit(TOGGLE_SIDEBAR, false)
  }
  next()
})
// 获取filter所有的属性,添加到vue.filter
// vue.filter : Vue.js 允许你自定义过滤器，可被用于一些常见的文本格式化
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

const i18n = new VueI18n({
  // 定义默认语言
  locale: 'zhCHS',
  messages: {
    'en': LangEn,
    'zhCHS': LangZhCHS
  }
})
// 初始化
const app = new Vue({
  router,
  i18n,
  store,
  nprogress,
  ...App
})

export { app, router, store }
